package com.movitech.mobile.cz.base.modules.activiti.service;

import java.util.Collection;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ActivitiRepositoryService {
    protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private RepositoryService repositoryService;

	/**
	 * 发布流程到流程引擎
	 * @param bpmnModel
	 * @param fileName
	 * @param deploymentName
	 * @return
	 */
	public Deployment deployBpmnModel(BpmnModel bpmnModel,String fileName,String deploymentName) {
		return repositoryService.createDeployment().addBpmnModel(fileName+".bpmn", bpmnModel).name(deploymentName).deploy();
	}
	
	/****
	 * 获取流程定义中所有的节点
	 * @param aProcessConfigProcessKey
	 * @return
	 */
	public Collection<FlowElement> getBpmnModelFlowElementList(String aProcessConfigProcessKey) {
		Collection<FlowElement> collectionFlowElements = null;
		BpmnModel model = repositoryService.getBpmnModel(aProcessConfigProcessKey);
		if(model != null) {  
			collectionFlowElements = model.getMainProcess().getFlowElements();  
		    for(FlowElement flowElement : collectionFlowElements) {  
		        System.out.println("flowelement id:" + flowElement.getId() + "  name:" + flowElement.getName() + "   class:" + flowElement.getClass().toString());  
		    }  
		}  
		return collectionFlowElements;
	}
}
